import React from 'react';
import {
    Row,
    Col,
    Table,
    Button,
    Form,
    message
} from 'antd';
import axios from 'axios';
import intercept from '../../utils/intercept';
import { config } from 'utils';
import format from '../BusStation/RentAdmin/format';
import './balance.less';

class CustomBusPlan extends React.Component {
    constructor(props) {
        super(props);
        const user = JSON.parse(sessionStorage.getItem("user"));
        this.state = {
            dataSource: [],
            loading: true,
            pagination: {
                current: 1,
                pageSize: 10
            },
            onChange: this.onChange,
            keyword: '',
            user: user,
            missMachine: [],
            stationstate: true,
            machinestate: true
        };
    };

    componentDidMount() {
        this.getMissStation();
        this.getMissMachine()
    };

    getMissMachine = () => {
        const token = sessionStorage.getItem("token");
        axios.get(`${config.api}DailyStatics/TerminalErrorMessage`, { headers: { Authorization: 'Bearer ' + token } }).then((data) => {
            console.log(data)
            if (data.data.result) {
                this.setState({ missMachine: data.data.errorTerminals, machinestate: false });
            } else {
                this.setState({ machinestate: false });
                message.warning(data.data.errorMessage)
            }
        })
        .catch(err => {
            intercept
        })
    }

    getMissStation = () => {
        const token = sessionStorage.getItem("token");
        axios.get(`${config.api}DailyStatics/OffLineStation`, { headers: { Authorization: 'Bearer ' + token } }).then((data) => {
            if (data.data.result) {
                this.setState({ loading: false, dataSource: data.data.offLineStations, stationstate: false });
            } else {
                this.setState({ stationstate: false });
                message.warning(data.data.errorMessage)
            }
        })
        .catch(err => {
            intercept
        })
    }

    clearError = (code) => {
        console.log(code)
        const token = sessionStorage.getItem("token");
        axios.post(`${config.api}DailyStatics/CleanTerminalError`,
            { machinelCode: code },
            { headers: { Authorization: 'Bearer ' + token } })
            .then((data) => {
                console.log(data)
                this.getMissMachine();
                if (data.data.result) {
                    message.success('清除成功')
                } else {
                    message.warning(data.data.errorMessage)
                }
            })
            .catch(err => {
                intercept
            })
    }

    MissStationNull(data) {
        if (data.length == 0) {
            return (
                <h3 style={{ color: 'red' }}>一切正常</h3>
            )
        } else {
            <Button type="danger">一切</Button>

        }
    }


    render() {
        const columns = [
            {
                title: "车站名称",
                dataIndex: "stationName",
                width: 200
            }, {
                title: "是否售票",
                dataIndex: "isAllow",
                width: 70,
                render: (text, record) => {
                    return (
                        <span style={record.isAllow ? { color: '#108EE9' } : { color: 'red' }}>{record.isAllow ? '售' : '否'}</span>
                    )
                }
            }, {
                title: "车站编号",
                dataIndex: "globalCode",
                width: 120
            }, {
                title: "未取车票",
                dataIndex: "tickets",
                width: 100
            }, {
                title: "时间",
                dataIndex: "errorTime",
                width: 120
            }, {
                title: "错误原因",
                dataIndex: "reason",
                width: 120
            }, {
                title: "错误",
                dataIndex: "errorMessage",
                width: 350
            }
        ];

        const columnZZJError = [
            {
                title: "车站编码",
                dataIndex: "globalCode",
                width: 100
            }, {
                title: "车站名称",
                dataIndex: "stationName",
                width: 120
            }, {
                title: "自助机编号",
                dataIndex: "machinelCode",
                width: 100
            }, {
                title: "终端号",
                dataIndex: "terminalCode",
                width: 100
            }, {
                title: "报错时间",
                dataIndex: "errorTime",
                width: 150,
                render: (text, record) => {
                    return (
                        <span>{format.format(record.errorTime)}</span>
                    )
                }
            }, {
                title: "错误信息",
                dataIndex: "errorMessage",
                width: 350
            }, {
                title: "清除错误信息",
                width: 120,
                render: (text, record) => {
                    return (
                        <Button type="danger" onClick={(machinelCode) => {this.clearError(record.machinelCode) }}>清除</Button>
                    )
                }
            },
        ];
        const className = "components-table-demo-nested";
        const scroll = {
            y: 600
        };
        return (
            <div className="orderinfo">
                <div style={{ width: '100%' }}>
                    <h2 style={{ color: 'red', marginBottom: 10 }}>自助机错误</h2>
                    <Row>
                        <Col span={24}>
                            <Table
                                className="menuItem"
                                dataSource={this.state.missMachine}
                                rowKey={record => record.globalCode}
                                bordered={true}
                                columns={columnZZJError}
                                loading={this.state.machinestate}
                                scroll={scroll}
                            />
                        </Col>
                    </Row>
                </div>

                <div style={{ marginTop: 60, width: '100%' }}>
                    <h2 style={{ color: 'red', marginBottom: 10 }}>断网车站</h2>
                    <Row>
                        <Col span={24}>
                            <Table
                                className="menuItem"
                                dataSource={this.state.dataSource}
                                rowKey={record => record.globalCode}
                                bordered={true}
                                className={className}
                                columns={columns}
                                loading={this.state.stationstate}
                                scroll={scroll}
                            />
                            {/* {this.MissStationNull(this.state.dataSource)} */}
                        </Col>
                    </Row>
                </div>
            </div>
        )
    }
}
const CBusPlan = Form.create()(CustomBusPlan);
export default CBusPlan
